package com.niubi.yygh.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.niubi.yygh.cmn.listener.DictListener;
import com.niubi.yygh.cmn.mapper.DictMapper;
import com.niubi.yygh.cmn.service.DictService;
import com.niubi.yygh.common.exception.YyghException;
import com.niubi.yygh.model.cmn.Dict;
import com.niubi.yygh.vo.cmn.DictEeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

  @Autowired
  private DictListener dictListener;

  //@Cacheable(value = "dict")
  @Cacheable(value = "dict", key = "'selectIndexList'+#id")
  @Override
  public List<Dict> findChlidData(Long id) {
    QueryWrapper<Dict> wrapper = new QueryWrapper<>();
    wrapper.eq("parent_id",id);
    List<Dict> dictList = baseMapper.selectList(wrapper);
    for (Dict dict : dictList) {
      Long dictId = dict.getId();
      boolean isChild = this.isChildren(dictId);
      dict.setHasChildren(isChild);
    }
    return dictList;
  }

  private boolean isChildren(Long dictId) {
    QueryWrapper<Dict> wrapper = new QueryWrapper<>();
    wrapper.eq("parent_id",dictId);
    Integer count = baseMapper.selectCount(wrapper);
    return count>0;
  }

  @Override
  public void exportData(HttpServletResponse response) {
    try {
      //设置下载相关内容
      response.setContentType("application/vnd.ms-excel");
      response.setCharacterEncoding("utf-8");
      String fileName = URLEncoder.encode("数据字典", "UTF-8");
      response.setHeader("Content-disposition","attachment;filename="+fileName + ".xlsx");
      //使用easyexcel写操作过程
      List<Dict> list = baseMapper.selectList(null);
      List<DictEeVo> listVo = new ArrayList<>();
      for (Dict dict : list) {
        DictEeVo dictEeVo = new DictEeVo();
        BeanUtils.copyProperties(dict,dictEeVo);
        listVo.add(dictEeVo);
      }
      EasyExcel.write(response.getOutputStream(),DictEeVo.class)
              .sheet("数据字典")
              .doWrite(listVo);
    }catch (Exception e){
      throw new YyghException(20001,"导出失败");
    }
  }

  @Override
  public void importDictData(MultipartFile file) {
    try {
      EasyExcel.read(file.getInputStream(),DictEeVo.class,dictListener)
              .sheet()
              .doRead();
    }catch (Exception e) {
      e.printStackTrace();
    }
  }

  @Override
  public String getNameByCode(String value, String dictCode) {
    if (StringUtils.isEmpty(dictCode)){
      QueryWrapper<Dict> wrapper = new QueryWrapper<>();
      wrapper.eq("value",value);
      Dict dict = baseMapper.selectOne(wrapper);
      if (dict!=null){
        return dict.getName();
      }
    }else {
      Dict dictParent = this.getParentIdByDictCode(dictCode);
      QueryWrapper<Dict> wrapper = new QueryWrapper<>();
      wrapper.eq("parent_id",dictParent.getId());
      wrapper.eq("value",value);
      Dict dict = baseMapper.selectOne(wrapper);
      if (dict !=null){
        return dict.getName();
      }
    }
    return null;
  }

  @Override
  public List<Dict> getByDictCode(String dictCode) {
    Dict dictParent = this.getParentIdByDictCode(dictCode);
    QueryWrapper<Dict> wrapper = new QueryWrapper<>();
    wrapper.eq("parent_id",dictParent.getId());
    List<Dict> list = baseMapper.selectList(wrapper);
    return list;
  }

  private Dict getParentIdByDictCode(String dictCode) {
    QueryWrapper<Dict> wrapper = new QueryWrapper<>();
    wrapper.eq("dict_code",dictCode);
    Dict dict = baseMapper.selectOne(wrapper);
    return dict;
  }
}